<?php
namespace app\wxpro\controller;
use think\Db;
use think\Controller;
use app\service\orderService;
use app\service\chargeRuleService;
use app\service\wechatService;
class Order extends Controller{
	public function wxnotice(){
		
	}
    public function wxProOrderQuery(){
		$car_number = request()->get('car_number');
		$orderinfo=Db::name('car_order')->where('car_number',$car_number)->where('order_status',"0")->find();
		if(empty($orderinfo)){
			$arr["status"]="0";
			$arr["error"]="暂无车辆信息";
			return json_encode($arr);
		}else{
			$arr["status"]="1";
			$data["out_time"]=date('YmdHis');
			$result = (new chargeRuleService())->getmoney($orderinfo["out_parking_id"],$orderinfo["in_time"],$data['out_time']);
			if($result["errcode"]=="1"){
				$arr["status"]="0";
				$arr["error"]="收费规则错误";
				return json_encode($arr);
				$this->error('收费规则错误');
			}
			if($result["money"]=="0"){
				$arr["status"]="0";
				$arr["error"]="费用0元无法预交费";
				return json_encode($arr);
				$this->error('费用0元无法预交费');
			}
			$data['pay_money']=$result["money"];
			//$data['pay_money']=0.01;
			//停车时间,减去免费时长
			$data['in_duration'] = (new chargeRuleService())->gettime($orderinfo["in_time"],$data['out_time']);
			$data["id"]=$orderinfo["id"];
			$orderedit = (new orderService())->orderEdit($data);
			$orderidinfo = (new orderService())->orderIdInfo($data['id']);
			$property=(new orderService())->propertyInfo($orderidinfo["parking_id"]);
			$orderidinfo["merchant_logo"]="http://www.huiwang1688.com/parkinglogo/".$property["merchant_logo"];
			$arr["orderInfo"]=$orderidinfo;
			return json_encode($arr);
		}
    }
	public function orderinfo(){
		$car_number = request()->get('car_number');
		$orderinfo=Db::name('car_order')->where('car_number',$car_number)->where('order_status',"0")->find();
		if(empty($orderinfo)){
			$arr["status"]="0";
			$arr["error"]="暂无车辆信息";
			return json_encode($arr);
		}else{
			$arr["status"]="1";
			$data["out_time"]=date('YmdHis');
			$result = (new chargeRuleService())->getmoney($orderinfo["out_parking_id"],$orderinfo["in_time"],$data['out_time']);
			if($result["errcode"]=="1"){
				$arr["status"]="0";
				$arr["error"]="收费规则错误";
				return json_encode($arr);
				$this->error('收费规则错误');
			}
			if($result["money"]=="0"){
				$arr["status"]="0";
				$arr["error"]="费用0元无法预交费";
				return json_encode($arr);
				$this->error('费用0元无法预交费');
			}
			$data['pay_money']=$result["money"];
			//$data['pay_money']=0.01;
			//停车时间,减去免费时长
			$data['in_duration'] = (new chargeRuleService())->gettime($orderinfo["in_time"],$data['out_time']);
			$data["id"]=$orderinfo["id"];
			$orderedit = (new orderService())->orderEdit($data);
			$orderidinfo = (new orderService())->orderIdInfo($data['id']);
			$property=(new orderService())->propertyInfo($orderidinfo["parking_id"]);
			$orderidinfo["merchant_logo"]="http://www.huiwang1688.com/parkinglogo/".$property["merchant_logo"];
			$arr["orderInfo"]=$orderidinfo;
			return json_encode($arr);
		}
    }
	public function wxpay(){
		$user_id = request()->get('user_id');
		$orderid = request()->get('id');
		$orderIdInfo=(new orderService())->orderIdInfo($orderid);
		if($orderIdInfo["order_status"]){}
         $data['sub_openid']=$user_id;
        //生成订单
        $data['appid'] = 'wx352362f198d9c56b';//公众账号ID
        $data['mch_id'] = '1484539292';//商户号
        $data['sub_appid'] = 'wx6e64e3c7372be385';//小程序id 
		//$data['sub_mch_id'] = (new orderService())->subMchId($orderIdInfo['parking_id']);//子商户公众账号ID
		$data['sub_mch_id']="1500219082";
        $data['nonce_str'] = (new wechatService())->createNoncestr();//随机字符串
        $data['body'] = $orderIdInfo["parking_name"].'小程序缴费';//商品描述
        $data['out_trade_no'] =time().rand(100000,999999);//商户订单号
        $data['total_fee'] = $orderIdInfo["pay_money"]*100;//总金额
        $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"];//终端IP
        $data['notify_url'] = 'https://www.huiwang1688.com/index/order/wxnotice';//通知urlwww.sqzhsq.com
        $data['trade_type'] = 'JSAPI';
        $wxkey = 'asdfaASDFASDFASFasfsadf6546A84SA';    
        $data['sign'] = (new wechatService())->MakeSign($data,$wxkey);//签名
        
        $xml = (new wechatService())->ToXml($data);
        
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $payInfo = (new wechatService())->post_curl_data($url,$xml);
        
        $payInfo = json_decode(json_encode(simplexml_load_string($payInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        
        if($payInfo['return_code'] !== 'SUCCESS'){
            return json(['info' => $payInfo['return_msg']]);
        }else{
            $carorderpay["out_trade_no"]=$data['out_trade_no'];
			$carorderpay["pay_money"]=$orderIdInfo['pay_money'];
			$carorderpay["pay_body"]=$data['body'];
			$carorderpay["uid"]=$user_id;
			$carorderpay["order_id"]=$orderid;
			$carorderpay["car_number"]=$orderIdInfo['car_number'];
			$carorderpay["pay_type"]=1;
			$carorderpay["scenes"]=7;
			 
            $res=Db::table("car_order_pay")->insert($carorderpay);
            if($res){
                $jsApiParameters['appId'] = $payInfo['sub_appid'];//小程序appid
                $jsApiParameters['timeStamp'] = time();
                $jsApiParameters['nonceStr'] = (new wechatService())->createNoncestr();
                $jsApiParameters['package'] = 'prepay_id='.$payInfo['prepay_id'];
                $jsApiParameters['signType'] = 'MD5';
                $jsApiParameters['paySign'] = (new wechatService())->MakeSigns($jsApiParameters,$wxkey);
				
                $jsApiParameters['trade_no']=$payInfo['prepay_id'];
				
				$wxpro_form["ctime"]=time();
				$wxpro_form["openid"]=$user_id;
				$wxpro_form["form_id"]=$payInfo['prepay_id'];
				$wxpro_form["num"]=4;
				$res=Db::table("wxpro_form")->insert($wxpro_form);
                return json_encode($jsApiParameters);    
            }
            
        }
	}
	
	
    
}